#include "CGUIIconFactory.h"
#include "IGUIEnvironment.h"

namespace irr
{
namespace gui
{


CGUIIconFactory::CGUIIconFactory(IGUIEnvironment* env)
: Environment(env)
{
	#ifdef _DEBUG
	setDebugName("CGUIIconFactory");
	#endif

	// don't grab the gui environment here to prevent cyclic references

	IndexMap[0] = EGIEFT_ICON;
	IndexMap[1] = EGIEFT_ICON_SLOT;
	IndexMap[2] = EGIEFT_ICON_SLOT_WINDOW;
}

//! adds an element to the gui environment based on its type id
IGUIElement* CGUIIconFactory::addGUIElement(EGUI_ELEMENT_TYPE type, IGUIElement* parent)
{
	EGUI_ICON_FACTORY_ELEMENT_TYPE t = (EGUI_ICON_FACTORY_ELEMENT_TYPE)type;

	IGUIElement* ret=0;

	switch (t)
	{
		case EGIEFT_ICON:
			ret = addIcon(core::position2di(0,0), parent);
			break;

		case EGIEFT_ICON_SLOT:
			break;

		case EGIEFT_ICON_SLOT_WINDOW:
			break;

		default:
			break;
	}

	return ret;
}

//! adds an element to the environment based on its type name
IGUIElement* CGUIIconFactory::addGUIElement(const c8* typeName, IGUIElement* parent)
{
	/*
		here we create elements, add them to the manager, and then drop them
	*/

	IGUIElement* ret=0;

	EGUI_ICON_FACTORY_ELEMENT_TYPE t = getTypeFromName(typeName);

	ret = addGUIElement((EGUI_ELEMENT_TYPE)t, parent);


	return ret;
}

EGUI_ICON_FACTORY_ELEMENT_TYPE CGUIIconFactory::getTypeFromName(const c8* typeName)
{
	core::stringc elementType(typeName);

	for (s32 i = 0; i < EGIEFT_COUNT; i++)
	{
		if (elementType == GUIIconFactoryElementTypeNames[i])
			return IndexMap[i];
	}

	return (EGUI_ICON_FACTORY_ELEMENT_TYPE)0;
}

EGUI_ELEMENT_TYPE CGUIIconFactory::getCreateableGUIElementType(s32 idx)
{
	if (idx>=0 && idx < EGIEFT_COUNT)
		return (EGUI_ELEMENT_TYPE)IndexMap[idx];

	return (EGUI_ELEMENT_TYPE)0;
}

//! returns amount of element types this factory is able to create
s32 CGUIIconFactory::getCreatableGUIElementTypeCount()
{
	return EGIEFT_COUNT;
}


//! returns type name of a createable element type 
const c8* CGUIIconFactory::getCreateableGUIElementTypeName(s32 idx)
{
	if (idx>=0 && idx < EGIEFT_COUNT)
		return GUIIconFactoryElementTypeNames[idx];

	return 0;
}

//! returns type name of a createable element type 
const c8* CGUIIconFactory::getCreateableGUIElementTypeName(EGUI_ELEMENT_TYPE type)
{

	s32 idx=0;
	EGUI_ICON_FACTORY_ELEMENT_TYPE t = (EGUI_ICON_FACTORY_ELEMENT_TYPE)type;

	for (idx=0; idx<EGIEFT_COUNT; idx++)
	{
		if (IndexMap[idx] == t)
			return GUIIconFactoryElementTypeNames[idx];
	}

	return 0;
}

CGUIIcon* CGUIIconFactory::addIcon( core::position2di pos, IGUIElement* parent, IGUISpriteBank* sprites, 
								   wchar_t* iconText, s32 id)
{
	if (!parent)
		parent = Environment->getRootGUIElement();
	if (!sprites && Environment->getSkin())
		sprites = Environment->getSkin()->getSpriteBank();

	CGUIIcon* ret = new	CGUIIcon(Environment, parent, id, pos, sprites, iconText);
	
	// the environment now has the reference, so we can drop the element
	if (ret)
		ret->drop();

	return ret;

}

CGUIIconSlot* CGUIIconFactory::addIconSlot(core::rect<s32> rect, s32 slotGroupID, IGUIElement *parent, s32 id)
{
	if (!parent)
		parent = Environment->getRootGUIElement();

	CGUIIconSlot* ret = new	CGUIIconSlot(Environment, parent, id, rect, slotGroupID);
	
	// the environment now has the reference, so we can drop the element
	if (ret)
		ret->drop();

	return ret;

}



} // end namespace gui
} // end namespace irr

